/* -----------------------------------------------------------------------------
 * Parser.java
 * -----------------------------------------------------------------------------
 *
 * Producer : com.parse2.aparse.Parser 2.3
 * Produced : Wed Jul 17 16:23:20 CST 2013
 *
 * -----------------------------------------------------------------------------
 */
package com.netscenery.yang.abnf;
import java.util.Stack;
import java.util.Properties;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.IOException;

public class Parser
{
  private Parser() {}

  static public void main(String[] args)
  {
    Properties arguments = new Properties();
    String error = "";
    boolean ok = args.length > 0;

    if (ok)
    {
      arguments.setProperty("Trace", "Off");
      arguments.setProperty("Rule", "module-stmt");

      for (int i = 0; i < args.length; i++)
      {
        if (args[i].equals("-trace"))
          arguments.setProperty("Trace", "On");
        else if (args[i].equals("-visitor"))
          arguments.setProperty("Visitor", args[++i]);
        else if (args[i].equals("-file"))
          arguments.setProperty("File", args[++i]);
        else if (args[i].equals("-string"))
          arguments.setProperty("String", args[++i]);
        else if (args[i].equals("-rule"))
          arguments.setProperty("Rule", args[++i]);
        else
        {
          error = "unknown argument: " + args[i];
          ok = false;
        }
      }
    }

    if (ok)
    {
      if (arguments.getProperty("File") == null &&
          arguments.getProperty("String") == null)
      {
        error = "insufficient arguments: -file or -string required";
        ok = false;
      }
    }

    if (!ok)
    {
      System.out.println("error: " + error);
      System.out.println("usage: Parser [-rule rulename] [-trace] <-file file | -string string> [-visitor visitor]");
    }
    else
    {
      try
      {
        Rule rule = null;

        if (arguments.getProperty("File") != null)
        {
          rule = 
            parse(
              arguments.getProperty("Rule"), 
              new File(arguments.getProperty("File")), 
              arguments.getProperty("Trace").equals("On"));
        }
        else if (arguments.getProperty("String") != null)
        {
          rule = 
            parse(
              arguments.getProperty("Rule"), 
              arguments.getProperty("String"), 
              arguments.getProperty("Trace").equals("On"));
        }

        if (arguments.getProperty("Visitor") != null)
        {
          Visitor visitor = 
            (Visitor)Class.forName(arguments.getProperty("Visitor")).newInstance();
          rule.accept(visitor);
        }
      }
      catch (IllegalArgumentException e)
      {
        System.out.println("argument error: " + e.getMessage());
      }
      catch (IOException e)
      {
        System.out.println("io error: " + e.getMessage());
      }
      catch (ParserException e)
      {
        System.out.println("parser error: " + e.getMessage());
      }
      catch (ClassNotFoundException e)
      {
        System.out.println("visitor error: class not found - " + e.getMessage());
      }
      catch (IllegalAccessException e)
      {
        System.out.println("visitor error: illegal access - " + e.getMessage());
      }
      catch (InstantiationException e)
      {
        System.out.println("visitor error: instantiation failure - " + e.getMessage());
      }
    }
  }

  static public Rule parse(String rulename, String string)
  throws IllegalArgumentException,
         ParserException
  {
    return parse(rulename, string, false);
  }

  static public Rule parse(String rulename, InputStream in)
  throws IllegalArgumentException,
         IOException,
         ParserException
  {
    return parse(rulename, in, false);
  }

  static public Rule parse(String rulename, File file)
  throws IllegalArgumentException,
         IOException,
         ParserException
  {
    return parse(rulename, file, false);
  }

  static private Rule parse(String rulename, String string, boolean trace)
  throws IllegalArgumentException,
         ParserException
  {
    if (rulename == null)
      throw new IllegalArgumentException("null rulename");
    if (string == null)
      throw new IllegalArgumentException("null string");

    ParserContext context = new ParserContext(string, trace);

    Rule rule = null;
    if (rulename.equalsIgnoreCase("module-stmt")) rule = Rule_module_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("submodule-stmt")) rule = Rule_submodule_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("module-header-stmts")) rule = Rule_module_header_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("submodule-header-stmts")) rule = Rule_submodule_header_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("meta-stmts")) rule = Rule_meta_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("linkage-stmts")) rule = Rule_linkage_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("revision-stmts")) rule = Rule_revision_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("body-stmts")) rule = Rule_body_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("data-def-stmt")) rule = Rule_data_def_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("yang-version-stmt")) rule = Rule_yang_version_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("yang-version-arg-str")) rule = Rule_yang_version_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("yang-version-arg")) rule = Rule_yang_version_arg.parse(context);
    else if (rulename.equalsIgnoreCase("import-stmt")) rule = Rule_import_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("include-stmt")) rule = Rule_include_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("namespace-stmt")) rule = Rule_namespace_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("uri-str")) rule = Rule_uri_str.parse(context);
    else if (rulename.equalsIgnoreCase("prefix-stmt")) rule = Rule_prefix_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("belongs-to-stmt")) rule = Rule_belongs_to_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("organization-stmt")) rule = Rule_organization_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("contact-stmt")) rule = Rule_contact_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("description-stmt")) rule = Rule_description_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("reference-stmt")) rule = Rule_reference_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("units-stmt")) rule = Rule_units_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("revision-stmt")) rule = Rule_revision_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("revision-date")) rule = Rule_revision_date.parse(context);
    else if (rulename.equalsIgnoreCase("revision-date-stmt")) rule = Rule_revision_date_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("extension-stmt")) rule = Rule_extension_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("argument-stmt")) rule = Rule_argument_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("yin-element-stmt")) rule = Rule_yin_element_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("yin-element-arg-str")) rule = Rule_yin_element_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("yin-element-arg")) rule = Rule_yin_element_arg.parse(context);
    else if (rulename.equalsIgnoreCase("identity-stmt")) rule = Rule_identity_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("base-stmt")) rule = Rule_base_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("feature-stmt")) rule = Rule_feature_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("if-feature-stmt")) rule = Rule_if_feature_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("typedef-stmt")) rule = Rule_typedef_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("type-stmt")) rule = Rule_type_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("type-body-stmts")) rule = Rule_type_body_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("numerical-restrictions")) rule = Rule_numerical_restrictions.parse(context);
    else if (rulename.equalsIgnoreCase("range-stmt")) rule = Rule_range_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("decimal64-specification")) rule = Rule_decimal64_specification.parse(context);
    else if (rulename.equalsIgnoreCase("fraction-digits-stmt")) rule = Rule_fraction_digits_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("fraction-digits-arg-str")) rule = Rule_fraction_digits_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("fraction-digits-arg")) rule = Rule_fraction_digits_arg.parse(context);
    else if (rulename.equalsIgnoreCase("string-restrictions")) rule = Rule_string_restrictions.parse(context);
    else if (rulename.equalsIgnoreCase("length-stmt")) rule = Rule_length_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("pattern-stmt")) rule = Rule_pattern_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("default-stmt")) rule = Rule_default_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("enum-specification")) rule = Rule_enum_specification.parse(context);
    else if (rulename.equalsIgnoreCase("enum-stmt")) rule = Rule_enum_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("leafref-specification")) rule = Rule_leafref_specification.parse(context);
    else if (rulename.equalsIgnoreCase("path-stmt")) rule = Rule_path_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("require-instance-stmt")) rule = Rule_require_instance_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("require-instance-arg-str")) rule = Rule_require_instance_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("require-instance-arg")) rule = Rule_require_instance_arg.parse(context);
    else if (rulename.equalsIgnoreCase("instance-identifier-specification")) rule = Rule_instance_identifier_specification.parse(context);
    else if (rulename.equalsIgnoreCase("identityref-specification")) rule = Rule_identityref_specification.parse(context);
    else if (rulename.equalsIgnoreCase("union-specification")) rule = Rule_union_specification.parse(context);
    else if (rulename.equalsIgnoreCase("bits-specification")) rule = Rule_bits_specification.parse(context);
    else if (rulename.equalsIgnoreCase("bit-stmt")) rule = Rule_bit_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("position-stmt")) rule = Rule_position_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("position-value-arg-str")) rule = Rule_position_value_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("position-value-arg")) rule = Rule_position_value_arg.parse(context);
    else if (rulename.equalsIgnoreCase("status-stmt")) rule = Rule_status_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("status-arg-str")) rule = Rule_status_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("status-arg")) rule = Rule_status_arg.parse(context);
    else if (rulename.equalsIgnoreCase("config-stmt")) rule = Rule_config_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("config-arg-str")) rule = Rule_config_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("config-arg")) rule = Rule_config_arg.parse(context);
    else if (rulename.equalsIgnoreCase("mandatory-stmt")) rule = Rule_mandatory_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("mandatory-arg-str")) rule = Rule_mandatory_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("mandatory-arg")) rule = Rule_mandatory_arg.parse(context);
    else if (rulename.equalsIgnoreCase("presence-stmt")) rule = Rule_presence_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("ordered-by-stmt")) rule = Rule_ordered_by_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("ordered-by-arg-str")) rule = Rule_ordered_by_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("ordered-by-arg")) rule = Rule_ordered_by_arg.parse(context);
    else if (rulename.equalsIgnoreCase("must-stmt")) rule = Rule_must_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("error-message-stmt")) rule = Rule_error_message_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("error-app-tag-stmt")) rule = Rule_error_app_tag_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("min-elements-stmt")) rule = Rule_min_elements_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("min-value-arg-str")) rule = Rule_min_value_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("min-value-arg")) rule = Rule_min_value_arg.parse(context);
    else if (rulename.equalsIgnoreCase("max-elements-stmt")) rule = Rule_max_elements_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("max-value-arg-str")) rule = Rule_max_value_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("max-value-arg")) rule = Rule_max_value_arg.parse(context);
    else if (rulename.equalsIgnoreCase("value-stmt")) rule = Rule_value_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("grouping-stmt")) rule = Rule_grouping_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("container-stmt")) rule = Rule_container_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("leaf-stmt")) rule = Rule_leaf_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("leaf-list-stmt")) rule = Rule_leaf_list_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("list-stmt")) rule = Rule_list_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("key-stmt")) rule = Rule_key_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("key-arg-str")) rule = Rule_key_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("key-arg")) rule = Rule_key_arg.parse(context);
    else if (rulename.equalsIgnoreCase("unique-stmt")) rule = Rule_unique_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("unique-arg-str")) rule = Rule_unique_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("unique-arg")) rule = Rule_unique_arg.parse(context);
    else if (rulename.equalsIgnoreCase("choice-stmt")) rule = Rule_choice_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("short-case-stmt")) rule = Rule_short_case_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("case-stmt")) rule = Rule_case_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("anyxml-stmt")) rule = Rule_anyxml_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("uses-stmt")) rule = Rule_uses_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("refine-stmt")) rule = Rule_refine_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("refine-arg-str")) rule = Rule_refine_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("refine-arg")) rule = Rule_refine_arg.parse(context);
    else if (rulename.equalsIgnoreCase("refine-container-stmts")) rule = Rule_refine_container_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-leaf-stmts")) rule = Rule_refine_leaf_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-leaf-list-stmts")) rule = Rule_refine_leaf_list_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-list-stmts")) rule = Rule_refine_list_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-choice-stmts")) rule = Rule_refine_choice_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-case-stmts")) rule = Rule_refine_case_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("refine-anyxml-stmts")) rule = Rule_refine_anyxml_stmts.parse(context);
    else if (rulename.equalsIgnoreCase("uses-augment-stmt")) rule = Rule_uses_augment_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("uses-augment-arg-str")) rule = Rule_uses_augment_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("uses-augment-arg")) rule = Rule_uses_augment_arg.parse(context);
    else if (rulename.equalsIgnoreCase("augment-stmt")) rule = Rule_augment_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("augment-arg-str")) rule = Rule_augment_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("augment-arg")) rule = Rule_augment_arg.parse(context);
    else if (rulename.equalsIgnoreCase("unknown-statement")) rule = Rule_unknown_statement.parse(context);
    else if (rulename.equalsIgnoreCase("unknown-statement2")) rule = Rule_unknown_statement2.parse(context);
    else if (rulename.equalsIgnoreCase("when-stmt")) rule = Rule_when_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("rpc-stmt")) rule = Rule_rpc_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("input-stmt")) rule = Rule_input_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("output-stmt")) rule = Rule_output_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("notification-stmt")) rule = Rule_notification_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("deviation-stmt")) rule = Rule_deviation_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("deviation-arg-str")) rule = Rule_deviation_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("deviation-arg")) rule = Rule_deviation_arg.parse(context);
    else if (rulename.equalsIgnoreCase("deviate-not-supported-stmt")) rule = Rule_deviate_not_supported_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("deviate-add-stmt")) rule = Rule_deviate_add_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("deviate-delete-stmt")) rule = Rule_deviate_delete_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("deviate-replace-stmt")) rule = Rule_deviate_replace_stmt.parse(context);
    else if (rulename.equalsIgnoreCase("range-arg-str")) rule = Rule_range_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("range-arg")) rule = Rule_range_arg.parse(context);
    else if (rulename.equalsIgnoreCase("range-part")) rule = Rule_range_part.parse(context);
    else if (rulename.equalsIgnoreCase("range-boundary")) rule = Rule_range_boundary.parse(context);
    else if (rulename.equalsIgnoreCase("length-arg-str")) rule = Rule_length_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("length-arg")) rule = Rule_length_arg.parse(context);
    else if (rulename.equalsIgnoreCase("length-part")) rule = Rule_length_part.parse(context);
    else if (rulename.equalsIgnoreCase("length-boundary")) rule = Rule_length_boundary.parse(context);
    else if (rulename.equalsIgnoreCase("date-arg-str")) rule = Rule_date_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("date-arg")) rule = Rule_date_arg.parse(context);
    else if (rulename.equalsIgnoreCase("schema-nodeid")) rule = Rule_schema_nodeid.parse(context);
    else if (rulename.equalsIgnoreCase("absolute-schema-nodeid")) rule = Rule_absolute_schema_nodeid.parse(context);
    else if (rulename.equalsIgnoreCase("descendant-schema-nodeid")) rule = Rule_descendant_schema_nodeid.parse(context);
    else if (rulename.equalsIgnoreCase("node-identifier")) rule = Rule_node_identifier.parse(context);
    else if (rulename.equalsIgnoreCase("instance-identifier")) rule = Rule_instance_identifier.parse(context);
    else if (rulename.equalsIgnoreCase("predicate")) rule = Rule_predicate.parse(context);
    else if (rulename.equalsIgnoreCase("predicate-expr")) rule = Rule_predicate_expr.parse(context);
    else if (rulename.equalsIgnoreCase("pos")) rule = Rule_pos.parse(context);
    else if (rulename.equalsIgnoreCase("path-arg-str")) rule = Rule_path_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("path-arg")) rule = Rule_path_arg.parse(context);
    else if (rulename.equalsIgnoreCase("absolute-path")) rule = Rule_absolute_path.parse(context);
    else if (rulename.equalsIgnoreCase("relative-path")) rule = Rule_relative_path.parse(context);
    else if (rulename.equalsIgnoreCase("descendant-path")) rule = Rule_descendant_path.parse(context);
    else if (rulename.equalsIgnoreCase("path-predicate")) rule = Rule_path_predicate.parse(context);
    else if (rulename.equalsIgnoreCase("path-equality-expr")) rule = Rule_path_equality_expr.parse(context);
    else if (rulename.equalsIgnoreCase("path-key-expr")) rule = Rule_path_key_expr.parse(context);
    else if (rulename.equalsIgnoreCase("rel-path-keyexpr")) rule = Rule_rel_path_keyexpr.parse(context);
    else if (rulename.equalsIgnoreCase("anyxml-keyword")) rule = Rule_anyxml_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("argument-keyword")) rule = Rule_argument_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("augment-keyword")) rule = Rule_augment_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("base-keyword")) rule = Rule_base_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("belongs-to-keyword")) rule = Rule_belongs_to_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("bit-keyword")) rule = Rule_bit_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("case-keyword")) rule = Rule_case_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("choice-keyword")) rule = Rule_choice_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("config-keyword")) rule = Rule_config_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("contact-keyword")) rule = Rule_contact_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("container-keyword")) rule = Rule_container_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("default-keyword")) rule = Rule_default_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("description-keyword")) rule = Rule_description_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("enum-keyword")) rule = Rule_enum_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("error-app-tag-keyword")) rule = Rule_error_app_tag_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("error-message-keyword")) rule = Rule_error_message_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("extension-keyword")) rule = Rule_extension_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("deviation-keyword")) rule = Rule_deviation_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("deviate-keyword")) rule = Rule_deviate_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("feature-keyword")) rule = Rule_feature_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("fraction-digits-keyword")) rule = Rule_fraction_digits_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("grouping-keyword")) rule = Rule_grouping_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("identity-keyword")) rule = Rule_identity_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("if-feature-keyword")) rule = Rule_if_feature_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("import-keyword")) rule = Rule_import_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("include-keyword")) rule = Rule_include_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("input-keyword")) rule = Rule_input_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("key-keyword")) rule = Rule_key_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("leaf-keyword")) rule = Rule_leaf_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("leaf-list-keyword")) rule = Rule_leaf_list_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("length-keyword")) rule = Rule_length_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("list-keyword")) rule = Rule_list_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("mandatory-keyword")) rule = Rule_mandatory_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("max-elements-keyword")) rule = Rule_max_elements_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("min-elements-keyword")) rule = Rule_min_elements_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("module-keyword")) rule = Rule_module_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("must-keyword")) rule = Rule_must_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("namespace-keyword")) rule = Rule_namespace_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("notification-keyword")) rule = Rule_notification_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("ordered-by-keyword")) rule = Rule_ordered_by_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("organization-keyword")) rule = Rule_organization_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("output-keyword")) rule = Rule_output_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("path-keyword")) rule = Rule_path_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("pattern-keyword")) rule = Rule_pattern_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("position-keyword")) rule = Rule_position_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("prefix-keyword")) rule = Rule_prefix_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("presence-keyword")) rule = Rule_presence_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("range-keyword")) rule = Rule_range_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("reference-keyword")) rule = Rule_reference_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("refine-keyword")) rule = Rule_refine_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("require-instance-keyword")) rule = Rule_require_instance_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("revision-keyword")) rule = Rule_revision_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("revision-date-keyword")) rule = Rule_revision_date_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("rpc-keyword")) rule = Rule_rpc_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("status-keyword")) rule = Rule_status_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("submodule-keyword")) rule = Rule_submodule_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("type-keyword")) rule = Rule_type_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("typedef-keyword")) rule = Rule_typedef_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("unique-keyword")) rule = Rule_unique_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("units-keyword")) rule = Rule_units_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("uses-keyword")) rule = Rule_uses_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("value-keyword")) rule = Rule_value_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("when-keyword")) rule = Rule_when_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("yang-version-keyword")) rule = Rule_yang_version_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("yin-element-keyword")) rule = Rule_yin_element_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("add-keyword")) rule = Rule_add_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("current-keyword")) rule = Rule_current_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("delete-keyword")) rule = Rule_delete_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("deprecated-keyword")) rule = Rule_deprecated_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("false-keyword")) rule = Rule_false_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("max-keyword")) rule = Rule_max_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("min-keyword")) rule = Rule_min_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("not-supported-keyword")) rule = Rule_not_supported_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("obsolete-keyword")) rule = Rule_obsolete_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("replace-keyword")) rule = Rule_replace_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("system-keyword")) rule = Rule_system_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("true-keyword")) rule = Rule_true_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("unbounded-keyword")) rule = Rule_unbounded_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("user-keyword")) rule = Rule_user_keyword.parse(context);
    else if (rulename.equalsIgnoreCase("current-function-invocation")) rule = Rule_current_function_invocation.parse(context);
    else if (rulename.equalsIgnoreCase("prefix-arg-str")) rule = Rule_prefix_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("prefix-arg")) rule = Rule_prefix_arg.parse(context);
    else if (rulename.equalsIgnoreCase("prefix")) rule = Rule_prefix.parse(context);
    else if (rulename.equalsIgnoreCase("identifier-arg-str")) rule = Rule_identifier_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("identifier-arg")) rule = Rule_identifier_arg.parse(context);
    else if (rulename.equalsIgnoreCase("identifier")) rule = Rule_identifier.parse(context);
    else if (rulename.equalsIgnoreCase("identifier-ref-arg-str")) rule = Rule_identifier_ref_arg_str.parse(context);
    else if (rulename.equalsIgnoreCase("identifier-ref-arg")) rule = Rule_identifier_ref_arg.parse(context);
    else if (rulename.equalsIgnoreCase("string")) rule = Rule_string.parse(context);
    else if (rulename.equalsIgnoreCase("integer-value")) rule = Rule_integer_value.parse(context);
    else if (rulename.equalsIgnoreCase("non-negative-integer-value")) rule = Rule_non_negative_integer_value.parse(context);
    else if (rulename.equalsIgnoreCase("positive-integer-value")) rule = Rule_positive_integer_value.parse(context);
    else if (rulename.equalsIgnoreCase("zero-integer-value")) rule = Rule_zero_integer_value.parse(context);
    else if (rulename.equalsIgnoreCase("stmtend")) rule = Rule_stmtend.parse(context);
    else if (rulename.equalsIgnoreCase("sep")) rule = Rule_sep.parse(context);
    else if (rulename.equalsIgnoreCase("optsep")) rule = Rule_optsep.parse(context);
    else if (rulename.equalsIgnoreCase("stmtsep")) rule = Rule_stmtsep.parse(context);
    else if (rulename.equalsIgnoreCase("line-break")) rule = Rule_line_break.parse(context);
    else if (rulename.equalsIgnoreCase("non-zero-digit")) rule = Rule_non_zero_digit.parse(context);
    else if (rulename.equalsIgnoreCase("decimal-value")) rule = Rule_decimal_value.parse(context);
    else if (rulename.equalsIgnoreCase("SQUOTE")) rule = Rule_SQUOTE.parse(context);
    else if (rulename.equalsIgnoreCase("ALPHA")) rule = Rule_ALPHA.parse(context);
    else if (rulename.equalsIgnoreCase("CR")) rule = Rule_CR.parse(context);
    else if (rulename.equalsIgnoreCase("CRLF")) rule = Rule_CRLF.parse(context);
    else if (rulename.equalsIgnoreCase("DIGIT")) rule = Rule_DIGIT.parse(context);
    else if (rulename.equalsIgnoreCase("DQUOTE")) rule = Rule_DQUOTE.parse(context);
    else if (rulename.equalsIgnoreCase("HEXDIG")) rule = Rule_HEXDIG.parse(context);
    else if (rulename.equalsIgnoreCase("HTAB")) rule = Rule_HTAB.parse(context);
    else if (rulename.equalsIgnoreCase("LF")) rule = Rule_LF.parse(context);
    else if (rulename.equalsIgnoreCase("SP")) rule = Rule_SP.parse(context);
    else if (rulename.equalsIgnoreCase("VCHAR")) rule = Rule_VCHAR.parse(context);
    else if (rulename.equalsIgnoreCase("WSP")) rule = Rule_WSP.parse(context);
    else if (rulename.equalsIgnoreCase("URI")) rule = Rule_URI.parse(context);
    else if (rulename.equalsIgnoreCase("hier-part")) rule = Rule_hier_part.parse(context);
    else if (rulename.equalsIgnoreCase("URI-reference")) rule = Rule_URI_reference.parse(context);
    else if (rulename.equalsIgnoreCase("absolute-URI")) rule = Rule_absolute_URI.parse(context);
    else if (rulename.equalsIgnoreCase("relative-ref")) rule = Rule_relative_ref.parse(context);
    else if (rulename.equalsIgnoreCase("relative-part")) rule = Rule_relative_part.parse(context);
    else if (rulename.equalsIgnoreCase("scheme")) rule = Rule_scheme.parse(context);
    else if (rulename.equalsIgnoreCase("authority")) rule = Rule_authority.parse(context);
    else if (rulename.equalsIgnoreCase("userinfo")) rule = Rule_userinfo.parse(context);
    else if (rulename.equalsIgnoreCase("host")) rule = Rule_host.parse(context);
    else if (rulename.equalsIgnoreCase("port")) rule = Rule_port.parse(context);
    else if (rulename.equalsIgnoreCase("IP-literal")) rule = Rule_IP_literal.parse(context);
    else if (rulename.equalsIgnoreCase("IPvFuture")) rule = Rule_IPvFuture.parse(context);
    else if (rulename.equalsIgnoreCase("IPv6address")) rule = Rule_IPv6address.parse(context);
    else if (rulename.equalsIgnoreCase("h16")) rule = Rule_h16.parse(context);
    else if (rulename.equalsIgnoreCase("ls32")) rule = Rule_ls32.parse(context);
    else if (rulename.equalsIgnoreCase("IPv4address")) rule = Rule_IPv4address.parse(context);
    else if (rulename.equalsIgnoreCase("dec-octet")) rule = Rule_dec_octet.parse(context);
    else if (rulename.equalsIgnoreCase("reg-name")) rule = Rule_reg_name.parse(context);
    else if (rulename.equalsIgnoreCase("path")) rule = Rule_path.parse(context);
    else if (rulename.equalsIgnoreCase("path-abempty")) rule = Rule_path_abempty.parse(context);
    else if (rulename.equalsIgnoreCase("path-absolute")) rule = Rule_path_absolute.parse(context);
    else if (rulename.equalsIgnoreCase("path-noscheme")) rule = Rule_path_noscheme.parse(context);
    else if (rulename.equalsIgnoreCase("path-rootless")) rule = Rule_path_rootless.parse(context);
    else if (rulename.equalsIgnoreCase("path-empty")) rule = Rule_path_empty.parse(context);
    else if (rulename.equalsIgnoreCase("segment")) rule = Rule_segment.parse(context);
    else if (rulename.equalsIgnoreCase("segment-nz")) rule = Rule_segment_nz.parse(context);
    else if (rulename.equalsIgnoreCase("segment-nz-nc")) rule = Rule_segment_nz_nc.parse(context);
    else if (rulename.equalsIgnoreCase("pchar")) rule = Rule_pchar.parse(context);
    else if (rulename.equalsIgnoreCase("query")) rule = Rule_query.parse(context);
    else if (rulename.equalsIgnoreCase("fragment")) rule = Rule_fragment.parse(context);
    else if (rulename.equalsIgnoreCase("pct-encoded")) rule = Rule_pct_encoded.parse(context);
    else if (rulename.equalsIgnoreCase("unreserved")) rule = Rule_unreserved.parse(context);
    else if (rulename.equalsIgnoreCase("reserved")) rule = Rule_reserved.parse(context);
    else if (rulename.equalsIgnoreCase("gen-delims")) rule = Rule_gen_delims.parse(context);
    else if (rulename.equalsIgnoreCase("sub-delims")) rule = Rule_sub_delims.parse(context);
    else throw new IllegalArgumentException("unknown rule");

    if (rule == null)
    {
      throw new ParserException(
        "rule \"" + (String)context.getErrorStack().peek() + "\" failed",
        context.text,
        context.getErrorIndex(),
        context.getErrorStack());
    }

    if (context.text.length() > context.index)
    {
      ParserException primaryError = 
        new ParserException(
          "extra data found",
          context.text,
          context.index,
          new Stack<String>());

      if (context.getErrorIndex() > context.index)
      {
        ParserException secondaryError = 
          new ParserException(
            "rule \"" + (String)context.getErrorStack().peek() + "\" failed",
            context.text,
            context.getErrorIndex(),
            context.getErrorStack());

        primaryError.initCause(secondaryError);
      }

      throw primaryError;
    }

    return rule;
  }

  static private Rule parse(String rulename, InputStream in, boolean trace)
  throws IllegalArgumentException,
         IOException,
         ParserException
  {
    if (rulename == null)
      throw new IllegalArgumentException("null rulename");
    if (in == null)
      throw new IllegalArgumentException("null input stream");

    int ch = 0;
    StringBuffer out = new StringBuffer();
    while ((ch = in.read()) != -1)
      out.append((char)ch);

    return parse(rulename, out.toString(), trace);
  }

  static private Rule parse(String rulename, File file, boolean trace)
  throws IllegalArgumentException,
         IOException,
         ParserException
  {
    if (rulename == null)
      throw new IllegalArgumentException("null rulename");
    if (file == null)
      throw new IllegalArgumentException("null file");

    BufferedReader in = new BufferedReader(new FileReader(file));
    int ch = 0;
    StringBuffer out = new StringBuffer();
    while ((ch = in.read()) != -1)
      out.append((char)ch);

    in.close();

    return parse(rulename, out.toString(), trace);
  }
}

/* -----------------------------------------------------------------------------
 * eof
 * -----------------------------------------------------------------------------
 */
